package cn.clq.org.controller;

import cn.clq.basic.query.BaseQuery;
import cn.clq.basic.util.AjaxResult;
import cn.clq.basic.util.PageList;
import cn.clq.org.domain.DictionaryDetail;
import cn.clq.org.query.DictionaryDetailQuery;
import cn.clq.org.service.IDictionaryDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author clqlongbao
 * @version 1.0
 * @description: 数据字典类型表的接口
 * @date 2022/6/5 9:49
 */
@RestController
@RequestMapping("/dicd")
public class DictionaryDetailController {

    @Autowired
    private IDictionaryDetailService dictionaryDetailService;
    /**
     * @description:查询单个数据字典类型
     * @param: id
     * @return: cn.clq.org.domain.DictionaryDetail
     * @author clqlongbao
     * @date: 2022/6/5 9:53
     */
    @GetMapping("/{id}")
    public DictionaryDetail queryById(@PathVariable("id") Long id){
        return dictionaryDetailService.queryById(id);
    }

    /**
     * @description:查询全部数据字典类型
     * @param:
     * @return: java.util.List<cn.clq.org.domain.DictionaryDetail>
     * @author clqlongbao
     * @date: 2022/6/5 10:52
     */
    @GetMapping
    public List<DictionaryDetail> queryAll(){
        return dictionaryDetailService.queryAll();
    }
    /**
     * @description:高级分页查询
     * @param: dictionaryDetailQuery
     * @return: cn.clq.basic.util.PageList<cn.clq.org.domain.DictionaryDetail>
     * @author clqlongbao
     * @date: 2022/6/5 10:55
     */
    @PostMapping
    public PageList<DictionaryDetail> queryPage(@RequestBody DictionaryDetailQuery dictionaryDetailQuery){
        return dictionaryDetailService.queryPage(dictionaryDetailQuery);
    }
    /**
     * @description:根据id删除一个数据字典类型
     * @param: id
     * @return: cn.clq.basic.util.AjaxResult
     * @author clqlongbao
     * @date: 2022/6/5 11:01
     */
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            dictionaryDetailService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统瓦了");
        }
    }
    /**
     * @description:判断是新增还是修改的接口
     * @param: dictionaryDetail
     * @return: cn.clq.basic.util.AjaxResult
     * @author clqlongbao
     * @date: 2022/6/5 11:05
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody DictionaryDetail dictionaryDetail){
        try {
            if(dictionaryDetail.getId() == null){
                //新增
                dictionaryDetailService.save(dictionaryDetail);
            }else{
                //修改
                dictionaryDetailService.update(dictionaryDetail);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    /**
     * @description:批量删除
     * @param: ids
     * @return: cn.clq.basic.util.AjaxResult
     * @author clqlongbao
     * @date: 2022/6/6 18:22
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody Long[] ids){
        try {
            dictionaryDetailService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试！！！");
        }
    }
}
